package com.example.pcbcsmbackend.service;

import com.example.pcbcsmbackend.entity.Address;
import com.example.pcbcsmbackend.repository.AddressRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class AddressService {
    
    @Autowired
    private AddressRepository addressRepository;
    
    // 获取用户的所有地址
    public List<Address> getAddressesByUserId(Integer userId) {
        return addressRepository.findByUserId(userId);
    }
    
    // 添加地址
    @Transactional
    public Address addAddress(Integer userId, String address, String contactName, String contactPhone, boolean isDefault) {
        // 如果设置为默认地址，先将该用户的其他地址设为非默认
        if (isDefault) {
            List<Address> userAddresses = addressRepository.findByUserId(userId);
            for (Address addr : userAddresses) {
                if (addr.getIsDefault() == 1) {
                    addr.setIsDefault(0);
                    addressRepository.save(addr);
                }
            }
        }
        
        Address newAddress = new Address();
        newAddress.setUserId(userId);
        newAddress.setAddress(address);
        newAddress.setContactName(contactName);
        newAddress.setContactPhone(contactPhone);
        newAddress.setIsDefault(isDefault ? 1 : 0);
        
        return addressRepository.save(newAddress);
    }
    
    // 更新地址
    @Transactional
    public Address updateAddress(Integer addressId, String address, String contactName, String contactPhone, boolean isDefault) {
        Optional<Address> addressOpt = addressRepository.findById(addressId);
        if (!addressOpt.isPresent()) {
            throw new RuntimeException("地址不存在");
        }
        
        Address existingAddress = addressOpt.get();
        
        // 如果设置为默认地址，先将该用户的其他地址设为非默认
        if (isDefault) {
            List<Address> userAddresses = addressRepository.findByUserId(existingAddress.getUserId());
            for (Address addr : userAddresses) {
                if (addr.getIsDefault() == 1 && !addr.getAddressId().equals(addressId)) {
                    addr.setIsDefault(0);
                    addressRepository.save(addr);
                }
            }
        }
        
        existingAddress.setAddress(address);
        existingAddress.setContactName(contactName);
        existingAddress.setContactPhone(contactPhone);
        existingAddress.setIsDefault(isDefault ? 1 : 0);
        
        return addressRepository.save(existingAddress);
    }
    
    // 删除地址
    public void deleteAddress(Integer addressId) {
        if (!addressRepository.existsById(addressId)) {
            throw new RuntimeException("地址不存在");
        }
        addressRepository.deleteById(addressId);
    }
    
    // 设置默认地址
    @Transactional
    public void setDefaultAddress(Integer addressId) {
        Optional<Address> addressOpt = addressRepository.findById(addressId);
        if (!addressOpt.isPresent()) {
            throw new RuntimeException("地址不存在");
        }
        
        Address address = addressOpt.get();
        
        // 将该用户的其他地址设为非默认
        List<Address> userAddresses = addressRepository.findByUserId(address.getUserId());
        for (Address addr : userAddresses) {
            if (addr.getIsDefault() == 1) {
                addr.setIsDefault(0);
                addressRepository.save(addr);
            }
        }
        
        // 设置当前地址为默认
        address.setIsDefault(1);
        addressRepository.save(address);
    }
    
    // 根据ID获取地址
    public Optional<Address> findById(Integer addressId) {
        return addressRepository.findById(addressId);
    }
} 